import request from './request'
import { useUserStore } from '../store/modules/user'
import { OrderAPIRequestParam } from '@/typings/api/order'
import axios, { AxiosPromise } from 'axios'
import { UploadRawFile } from 'element-plus'

const cancelToken = axios.CancelToken
let cancel = () => {}

/**
 * 创建或修改工单保存
 * @param data - 请求参数
 */
export function addSave(data: OrderAPIRequest.OrderCreateForm) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/save',
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 获取工单模块下拉选项
 * @param {Object} data - 请求参数
 * @param {String} data.name - 搜索关键词
 * @param {Number} data.page - 当前页码
 * @param {Number} data.limit - 一页显示多少条
 */
export function workModulePageSelect(data: {
  limit: number
  name: string
  page: number
}): AxiosPromise<OrderAPIResponse.ModuleOptions> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/order/select/workModulePageSelect',
    method: 'post',
    headers: token,
    data,
  })
}

// 工单级别下拉列表查询
export function workLevelPageSelect(data: { limit: number; name: any; page: number }) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/order/select/workLevelPageSelect',
    method: 'post',
    headers: token,
    data,
  })
}
// 工单客服组下拉列表查询
export function workTeamPageSelect(data: { limit: number; name: any; page: number }) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/order/select/workTeamPageSelect',
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 分页获取公司下拉数据，带出所有联系人及对应的账号
 * @param {Object} data - 请求参数
 * @param {Number} data.limit - 一页显示多少条
 * @param {Number} data.page - 当前页码
 * @param {String} data.name - 搜索关键词
 */
export function getUserSelect(data: {
  limit: number
  name: string
  page: number
}): AxiosPromise<CustomerAPIResponse.CustomerOptionList> {
  return request({
    url: '/select/user/pageUserSelect',
    method: 'post',
    data,
  })
}

// 用户联系人下拉数据列表
export function userSelect(data: { limit: number; name: any; page: number }, userId: string) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/select/user/' + userId,
    method: 'post',
    headers: token,
    data,
  })
}
// 获取工单报单产品
export function getOrderProduct(data: {
  brandId: string
  keyword: string
  limit: number
  page: number
  typeId: string
  typeThreeId: string
  typeTwoId: string
}): AxiosPromise<OrderAPIResponse.GoodList> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/goods/product/getOrderProduct',
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 工单模块处理方式下拉列表查询
 * @param {Object} data - 请求参数
 * @param {Number} data.limit - 一页显示多少条
 * @param {Number} data.page - 当前页码
 * @param {String} data.name - 搜索关键字
 * @param {String} moduleId - 模块id
 */
export const workModuleWayPageSelect = (
  data: { limit: number; name: string; page: number },
  moduleId: string,
): AxiosPromise<OrderAPIResponse.ModuleHandleMethodOptions> => {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/order/select/workModuleWayPageSelect/' + moduleId,
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 工单模块分类下拉列表查询
 * @param {Object} data - 请求参数
 * @param {Number} data.limit - 一页显示多少条
 * @param {Number} data.page - 当前页码
 * @param {String} data.name - 搜索关键字
 * @param {String} moduleId - 模块id
 */
export const workModuleTypePageSelect = (
  data: { limit: number; name: string; page: number },
  moduleId: string,
): AxiosPromise<OrderAPIResponse.ModuleTypeOptions> => {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/order/select/workModuleTypePageSelect/' + moduleId,
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 获取工单列表
 * @param {OrderAPIRequest.GetList} data - 请求参数
 */
export function getOrderList(data: OrderAPIRequest.GetList) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/getList',
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 获取工单列表状态数量
 */
export function getOrderStatusQty(): AxiosPromise<OrderAPIResponse.ListStatusQty> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/getOrderStatusQty',
    method: 'get',
    headers: token,
  })
}

/**
 * 根据工单ID获取工单详情
 * @param {String} id - 工单id
 */
export function getOrderDetail(id: string): AxiosPromise<OrderAPIResponse.Detail> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/getOrderDetail/' + id,
    method: 'get',
    headers: token,
  })
}

/**
 * 根据模块ID获取工单自定义字段
 * @param {String} id - 工单模块id
 */
export function getModuleField(id: string): AxiosPromise<OrderAPIResponse.CustomFieldForCreate> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/module/field/getModuleField/' + id,
    method: 'get',
    headers: token,
  })
}

//获取工单列表搜索条件的工单自定义搜索字段
export function getOrderListSelectField() {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/getOrderListSelectField',
    method: 'get',
    headers: token,
  })
}
// 批量分配客服
export function dispatchOrderEmp(data: unknown[], empId: string) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/dispatchOrderEmp/' + empId,
    method: 'put',
    headers: token,
    data,
  })
}
// 批量更新工单流程为完成
export function finishOrder(data: { remark: string; workIds: unknown[] }) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/finishOrder',
    method: 'put',
    headers: token,
    data,
  })
}
//批量更新工单流程为取消
export function cancelOrder(data: { remark: string; workIds: unknown[] }) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/cancelOrder',
    method: 'put',
    headers: token,
    data,
  })
}
// 工单催单
export function saveReminder(data: { content: string; workIds: unknown[] }) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/saveReminder',
    method: 'post',
    headers: token,
    data,
  })
}
// 根据工单ID获取工单催单列表
export function getReminderList(data: { limit: number; page: number }, workId: string | unknown[]) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/getReminderList/' + workId,
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 根据工单ID获取工单设备
 * @param {string} workId 工单id
 * @param {Object} data 分页配置
 * @param {number} data.limit 一页显示多少条
 * @param {number} data.page 页码
 */
export function getOrderGoodsDetail(
  workId: string,
  data: { limit: number; page: number },
): AxiosPromise<APIResponseList<OrderDetailAPIResponse.GoodListItem>> {
  return request({
    url: '/work/order/getOrderGoodsDetail/' + workId,
    method: 'post',
    data,
  })
}

//根据工单ID获取工单操作记录列表
export function getOrderLog(data: { limit: number; page: number }, workId: string | unknown[]) {
  return request({
    url: '/work/order/getOrderLog/' + workId,
    method: 'post',
    data,
  })
}
// 工单预约时间
export function reservationTime(data: {
  date: string
  remark: string
  time: string
  workId: string | Record<string, any>
}) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/reservationTime',
    method: 'put',
    headers: token,
    data,
  })
}

/**
 * 派单给务商
 * @param {object} data - 请求参数
 * @param {string | undefined} data.networkEmpId - 部门人员ID
 * @param {string} data.networkId - 部门ID
 * @param {string | undefined} data.remark - 派单备注
 * @param {Array<string>} data.workIds - 工单ID
 */
export function orderDispatchNetwork(data: OrderAPIRequestParam.OrderDispatchNetwork) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/orderDispatchNetwork',
    method: 'put',
    headers: token,
    data,
  })
}

//根据工单ID获取工单留言
export function getWorkLeaveMessageList(
  data: { limit: number; page: number },
  workId: string | unknown[],
) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/getWorkLeaveMessageList/' + workId,
    method: 'post',
    headers: token,
    data,
  })
}
// 保存工单留言
export function saveWorkLeaveMessage(data: {
  annex: any[]
  content: string
  visitTypes: Array<any>
  workId: string | unknown[]
}) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/saveWorkLeaveMessage',
    method: 'post',
    headers: token,
    data,
  })
}
// 改派部门
export function changeNetwork(data: {
  changeToNetworkEmpId: any
  changeToNetworkId: any
  currentNetworkId: string
  remark: string
  workId: unknown
}) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/changeNetwork',
    method: 'put',
    headers: token,
    data,
  })
}
// 改派工单客服
export function changeServiceEmp(data: { empId: any; remark: string; workId: unknown }) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/changeServiceEmp',
    method: 'put',
    headers: token,
    data,
  })
}
// 根据工单ID获取部门信息
export function getServiceInfo(workId: string | string[]) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/getServiceInfo/' + workId,
    method: 'get',
    headers: token,
  })
}

/**
 * 获取工单列表表头的工单自定义字段
 */
export function getOrderListTableTitleField(): AxiosPromise<OrderAPIResponse.OrderListTableHeaderCustomFieldList> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/getOrderListTableTitleField',
    method: 'get',
    headers: token,
  })
}

/**
 * 根据工单ID获取服务项目列表
 * @param {Object} data - 请求参数
 * @param {Number} data.limit - 一页显示多少条
 * @param {Number} data.page - 页码
 * @param {String} workId - 工单id
 */
export function getOrderDetailFeeProjectList(
  data: { limit: number; page: number },
  workId: string,
): AxiosPromise<APIResponseList<OrderFeeAPIResponse.OrderDetailFeeListItem>> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/fee/getOrderDetailFeeProjectList/' + workId,
    method: 'post',
    headers: token,
    data,
  })
}

// 获取工单服务费用项目列表
export function feeWork(data: {
  applyEmp: string
  createEndTime: string
  createStartTime: string
  feeNo: string
  limit: number
  page: number
  status: any
  workNo: string
}) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/fee/getList',
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 根据服务项目ID获取项目信息列表
 * @param id - 费用id
 */
export function OrderFeeDetailProjectInfo(
  id: string,
): AxiosPromise<OrderAPIResponse.FeeProejctList> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/fee/getOrderFeeDetailProjectInfo/' + id,
    method: 'get',
    headers: token,
  })
}

/**
 * 审核工单服务费用项目
 * @param data - 请求参数
 */
export function checkOrderFee(data: OrderAPIRequest.FeeCreateForm): AxiosPromise<APIResponse> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/fee/checkOrderFee',
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 保存或修改工单服务费用
 * @param data - 请求参数
 */
export function feeSaveOrder(
  data: OrderFeeAPIRequest.OrderDetailFeeForm,
): AxiosPromise<APIResponse> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/fee/save',
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 工单费用项目下拉列表查询
 * @param {Object} data - 请求参数
 * @param {String} data.name - 搜索关键词
 * @param {String} data.limit - 每页显示数量
 * @param {String} data.page - 当面页码
 */
export function workFeeProjectPageSelect(data: {
  limit: number
  page: number
  name: string
}): AxiosPromise<OrderAPIResponse.FeeProjectOptionList> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/order/select/workFeeProjectPageSelect',
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 根据服务项目ID获取服务项目详情
 * @param id - 费用id
 */
export function getOrderFeeDetail(id: string): AxiosPromise<OrderAPIResponse.FeeDetail> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/fee/getOrderFeeDetail/' + id,
    method: 'get',
    headers: token,
  })
}

// 根据服务项目ID获取服费用项目日志列表
export function getOrderFeeLogList(data, id) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/fee/getOrderFeeLogList/' + id,
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 分页获取工单物料下拉数据列表
 * @param data - 请求参数
 * @param workId - 工单id
 */
export function getOrderSparePageSelect(
  data: { limit: number; page: number; name: string },
  workId: string,
): AxiosPromise<OrderAPIResponse.MaterialOptionList> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/order/select/getOrderSparePageSelect/' + workId,
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 分页获取工单物料规格下拉数据列表
 * @param data - 请求参数
 * @param materialId - 配件id
 */
export function getOrderSpareSpecSelect(
  data: { limit: number; page: number; name: string },
  materialId: string,
): AxiosPromise<OrderAPIResponse.MaterialSpecOptionList> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/order/select/getOrderSpareSpecSelect/' + materialId,
    method: 'post',
    headers: token,
    data,
  })
}

// 根据工单ID获取工单信息
export function getOrderParamField(workId) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/order/getOrderParamField/' + workId,
    method: 'get',
    headers: token,
  })
}

// 获取工单列表表头字段
export function getOrderListTableField(type) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/workTableField/getOrderListTableField/' + type,
    method: 'get',
    headers: token,
  })
}
// 获取工单表头字段设置项
export function getOrderTableField(type) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/workTableField/getOrderTableField/' + type,
    method: 'get',
    headers: token,
  })
}
// 保存或更新工单表头字段
export function workTableFieldSave(data, type) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/workTableField/save/' + type,
    method: 'post',
    headers: token,
    data,
  })
}
// 获取工单列表排序字段设置
export function getOrderTableOrderByField(type) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/orderBy/field/getOrderTableOrderByField/' + type,
    method: 'get',
    headers: token,
  })
}
// 新增或修改工单列表排序字段
export function orderBySave(data, type) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/orderBy/field/save/' + type,
    method: 'post',
    headers: token,
    data,
  })
}

//获取工单表头初始化字段
export function getOrderTableInitField() {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/workTableField/getOrderTableInitField',
    method: 'get',
    headers: token,
  })
}
//获取工单列表排序初始化字字段
export function getOrderTableInitOrderByField() {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/orderBy/field/getOrderTableInitOrderByField',
    method: 'get',
    headers: token,
  })
}
//工单模块带费用下拉列表查询
export function workModulePricePageSelect(data) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/order/select/workModulePricePageSelect',
    method: 'post',
    headers: token,
    data,
  })
}

// 获取工单所有导出字段
export function getOrderExportField() {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/export/field/getOrderExportField',
    method: 'get',
    headers: token,
  })
}

// 获取工单自定义字段
export function getOrderCustomField() {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/export/field/getOrderCustomField',
    method: 'get',
    headers: token,
  })
}

// 获取工单系统初始导出字段
export function getOrderSysExportFields() {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/export/field/getOrderSysExportFields',
    method: 'get',
    headers: token,
  })
}

// 保存导出字段
export function saveExportField(data) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/export/field/save',
    method: 'post',
    headers: token,
    data,
  })
}

// 保存并导出字段
export function saveAndExportField(data) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/export/field/saveAndExportField',
    method: 'post',
    headers: token,
    data,
    responseType: 'blob',
  })
}

/**
 * 获取工单详情的合计总费用
 * @param {string} id - 工单id
 */
export function getOrderTotalFee(id: string) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/work/order/getOrderTotalFee/${id}`,
    method: 'get',
    headers: token,
  })
}

/**
 * 发送结单核销码
 * @param {string} id 工单id
 * @returns {Promise<any>}
 */
export function sendOrderStatementWriteOffCode(id: string) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/work/order/sendOrderStatementWriteOffCode/${id}`,
    method: 'get',
    headers: token,
  })
}

/**
 * 获取待审核的工单事件
 * @param {string} id 工单ID
 */
export function getWaitCheckOrderEvent(
  id: string,
): AxiosPromise<OrderAPIResponse.WaitHanldeEventList> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/work/order/getWaitCheckOrderEvent/${id}`,
    method: 'get',
    headers: token,
  })
}

/**
 * 审核取消工单
 * @param {string} orderId - 工单id
 * @param {object} data - 请求参数
 * @param {1 | 2} data.checkType - 取消工单审核类型：1-审核通过，2-驳回
 * @param {string} data.id - 取消工单ID
 * @param {string} data.remark - 审核备注
 */
export function checkCancelOrder(orderId: string, data: OrderAPIRequestParam.CheckCancelOrder) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/work/order/checkCancelOrder/${orderId}`,
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 根据工单ID获取待审核取消工单
 * @param {string} id - 工单id
 */
export function getWaitCheckCancelOrder(id: string) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/work/order/getWaitCheckCancelOrder/${id}`,
    method: 'get',
    headers: token,
  })
}

/**
 * 审核结单
 * @param {string} id - 工单id
 * @param {object} data - 请求参数
 * @param {1 | 2} data.checkType - 结单审核类型：1-审核通过，2-驳回
 * @param {string} data.id - 工单结单ID
 * @param {number} data.price - 结单总费用
 * @param {string} data.remark - 审核结单备注
 * @param {number} data.surePrice - 确认结单费用
 */
export function checkStatementOrder(id: string, data: OrderAPIRequestParam.CheckStatementOrder) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/work/order/checkStatementOrder/${id}`,
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 根据工单ID获取工单待结单审核
 * @param {string} id - 工单id
 */
export function getWaitCheckStatementOrder(id: string) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/work/order/getWaitCheckStatementOrder/${id}`,
    method: 'get',
    headers: token,
  })
}

// 获取工单最新进展
export function getOrderLastLog(id: string) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/work/order/getOrderLastLog/${id}`,
    method: 'get',
    headers: token,
    cancelToken: new cancelToken((c) => {
      // 接收一个 cancel 函数作为参数
      cancel = c
    }),
  })
}

// 取消上一次请求获取工单最新进展
export function axiosCancel() {
  cancel()
}

/**
 * 保存工单备注
 * @param {object} data - 请求参数
 * @param {Array<any>} data.annexs - 工单备注附件
 * @param {string} data.remark - 工单备注
 * @param {0 | 1 | 2 | 3 | 4} data.visitTypes - 可见类型：0-公开，1-部门，2-工程师，3-工单公司，4-工单客服
 * @param {Array<string>} data.workIds - 工单ID参数
 */
export function saveRemark(data: OrderAPIRequestParam.SaveOrderRemark) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/ork/remark/save',
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 工单超时提醒
 * @param {Array<string>} data.remindEmpIds 指定人员ID
 * @param {Array<number>} data.remindEmpType 提醒人员类型：1-工单客服，2-部门负责人，3-工程师，4-指定人员
 * @param {Array<string>} data.workIds 工单ID
 */
export function workTimeoutRemind(data) {
  return request({
    url: '/work/order/workTimeoutRemind',
    method: 'post',

    data,
  })
}

/**
 * 获取预约时间范围
 * @param {string} workId - 工单id
 */
export function getReservationRange(workId: string) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/work/order/getReservationRange/${workId}`,
    method: 'get',
    headers: token,
  })
}

/**
 * 根据工单ID获取待审核改派部门的工单
 * @param {string} id - 工单id
 */
export function getWaitCheckWorkReassign(id: string) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/work/order/getWaitCheckWorkReassign/${id}`,
    method: 'get',
    headers: token,
  })
}

/**
 * 审核改派部门的工单
 * @param {Object} data - 请求参数
 * @param {string} data.changeToNetworkId - 新部门ID
 * @param {1 | 2} data.checkTypep - 审核类型：1-审核通过，2-驳回
 * @param {string} data.id - 改派工单ID
 * @param {string} data.networkEmpId - 新部门工程师ID
 * @param {string} data.remark - 备注
 * @param {string} workId - 工单id
 */
export function checkWorkReassign(data: OrderAPIRequestParam.CheckWorkReassign, workId: string) {
  return request({
    url: `/work/order/checkWorkReassign/${workId}`,
    method: 'post',
    data,
  })
}
// 根据工单ID获取结单字段
export function getOrderSettlementField(workId: any) {
  return request({
    url: `/work/order/getOrderSettlementField/${workId}`,
    method: 'get',
  })
}
/**获取历史工单列表 */
export function getHistoryOrderList(data) {
  return request({
    url: `/history/order/getHistoryOrderList`,
    method: 'post',
    data,
  })
}
/**获取历史工单详情 */
export function getHistoryDetail(id) {
  return request({
    url: `/history/order/getDetail/${id}`,
    method: 'get',
  })
}
/**获取历史工单设备*/
export function getHistoryOrderGoods(id, data) {
  return request({
    url: `/history/order/getHistoryOrderGoods/${id}`,
    method: 'post',
    data,
  })
}
/**批量删除历史工单*/
export function deleteHistoryOrder(data) {
  return request({
    url: `/history/order/deleteHistoryOrder`,
    method: 'DELETE',
    data,
  })
}

/**
 * 保存或修改服务费用标准
 * @param {OrderFeeStandardDetail} data - 收费标准详情
 */
export function serviceStandardSave(data: OrderFeeStandardDetail): AxiosPromise<APIResponse> {
  return request({
    url: '/work/service/fee/standard/save',
    method: 'post',
    data,
  })
}

/**获取服务费用列表*/
export function serviceStandardgetList(data): AxiosPromise<OrderAPIResponse.FeeStandards> {
  return request({
    url: `/work/service/fee/standard/getList`,
    method: 'post',
    data,
  })
}

/**
 * 获取服务费用标准详情
 * @param {String} id - 收费标准id
 */
export function standardgetDetail(id: string): AxiosPromise<OrderAPIResponse.FeeStandardDetail> {
  return request({
    url: `/work/service/fee/standard/getDetail/${id}`,
    method: 'get',
  })
}

/**
 * 批量更新是否移动端显示
 * @param {Array<string>} data - 收费标准id集合
 * @param {Boolean} status - 是否移动显示：false-隐藏，true-显示
 */
export function updateMobileShow(data: Array<string>, status: boolean): AxiosPromise<APIResponse> {
  return request({
    url: '/work/service/fee/standard/updateMobileShow',
    method: 'post',
    data,
    params: { mobileShow: status },
  })
}

/**
 * 收费标准批量删除
 * @param {Array<string>} data - 收费标准id集合
 */
export function feeStandardsDelete(data: Array<string>): AxiosPromise<APIResponse> {
  return request({
    url: '/work/service/fee/standard/delete',
    method: 'post',
    data,
  })
}

/**
 * 收费标准批量禁用或启用
 * @param {Array<string>} data - 收费标准id集合
 * @param {Boolean} status - false（启用），true（禁用）
 */
export function feeStandardsDisable(
  data: Array<string>,
  status: boolean,
): AxiosPromise<APIResponse> {
  return request({
    url: '/work/service/fee/standard/disable',
    method: 'post',
    data,
    params: { status },
  })
}

/**
 * 更改工单设备序列号
 * @param data - 请求参数
 * @param data.snCodes - 序列号
 * @param data.workGoodsId - 工单商品ID
 * @param workId - 工单ID
 */
export function updateWorkGoodsBindSnCode(
  data: OrderDetailAPiRequest.UpdateOrderDetailGoodSnCode,
  workId: string,
): AxiosPromise<APIResponse> {
  return request({
    url: `/work/order/updateWorkGoodsBindSnCode/${workId}`,
    method: 'POST',
    data,
  })
}

/**
 * 新增或修改结算方案
 * @param {Object} data - 请求参数
 */
export const goodSettleAddOrUpdate = (
  data: OrderAPIRequest.GoodSettleForm,
): AxiosPromise<APIResponse> => {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/goods/settlement/programme/save',
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 获取结算方案列表
 * @param {Object} data - 请求参数
 * @param {String} data.goodsKeyword - 设备搜索关键词：设备名称/设备编号/设备型号
 * @param {String} data.goodsOneTypeId - 一级设备分类ID
 * @param {String} data.goodsThreeTypeId - 三级设备分类ID
 * @param {String} data.goodsTwoTypeId - 二级设备分类ID
 * @param {Number} data.limit - 每页显示数量
 * @param {String} data.name - 方案名称
 * @param {Number} data.page - 当面页码
 */
export const getGoodSettles = (
  data: OrderAPIRequest.GoodSettles,
): AxiosPromise<OrderAPIResponse.OrderGoodSettleList> => {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/goods/settlement/programme/getList',
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 批量启用或禁用结算方案
 * @param {Array<string>} ids - 结算方案ID集合
 * @param {Boolean} status - 结算方案状态：false-启用，true-禁用
 */
export const goodSettlesUpdateStatus = (
  ids: Array<string>,
  status: boolean,
): AxiosPromise<APIResponse> => {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/goods/settlement/programme/disabled',
    method: 'post',
    headers: token,
    data: ids,
    params: {
      status,
    },
  })
}

/**
 * 批量删除结算方案
 * @param {Array<string>} ids - 结算方案ID集合
 */
export const goodSettlesDel = (ids: Array<string>): AxiosPromise<APIResponse> => {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/work/goods/settlement/programme/delete',
    method: 'post',
    headers: token,
    data: ids,
  })
}

/**
 * 根据id获取设备结算方案
 * @param {String} id - 设备结算方案id
 */
export const getGoodSettleDetailById = (
  id: string,
): AxiosPromise<OrderAPIResponse.GoodSettleDetail> => {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/work/goods/settlement/programme/getDetail/${id}`,
    method: 'get',
    headers: token,
  })
}
/**
 * 查询是否存在重单
 * @param {repeatOrder} data
 */
export function getRepeatOrder(data) {
  return request({
    url: `/work/order/getRepeatOrder`,
    method: 'post',
    data,
  })
}
/**
 * 获取查询重单设置
 */
export function getRepeatOrderSet() {
  return request({
    url: `/work/order/getRepeatOrderSet`,
    method: 'get',
  })
}

/**
 * 获取修改工单公司前的信息
 */
export function getBeforeUpdateWorkUserInfo(workId) {
  return request({
    url: `/work/order/getBeforeUpdateWorkUserInfo/${workId}`,
    method: 'get',
  })
}

/**
 * 保存更改的工单公司信息
 * @param {OrderDetailAPiRequest.OrderCustomerUpdateForm} data - 请求参数
 * @param {string} data.address - 客户详细地址
 * @param {string} data.area - 地址-县/区
 * @param {string} data.city - 地址-市
 * @param {0|1} data.helpOtherCreateOrder - 是否代他人建单：0-否，1-是
 * @param {string} data.name - 客户名称
 * @param {string} data.otherPhone - 其他电话
 * @param {string} data.phone - 公司联系方式
 * @param {string} data.province - 地址-省
 * @param {string} data.id - 工单id
 * @param {string} data.repairCompany - 报修客户名称
 * @param {string} data.repairCompanyId - 报修公司ID
 * @param {string} data.repairCompanyPhone - 报修公司联系方式
 * @param {string} data.sendForRepairPhone - 送修人联系手机号
 * @param {string} data.sendForRepairUsername - 送修人名称
 * @param {string} data.town - 地址-镇
 * @param {string} data.userId - 公司ID
 */
export function saveUpdateWorkUser(
  data: OrderDetailAPiRequest.OrderCustomerUpdateForm,
): AxiosPromise<APIResponse> {
  return request({
    url: `/work/order/saveUpdateWorkUser`,
    method: 'post',
    data,
  })
}

/**
 * 获取修改前的工单信息
 * @param {String} orderId - 工单id
 */
export function getBeforeUpdateWorkInfo(
  orderId: string,
): AxiosPromise<OrderAPIResponse.InfoBeforeEdit> {
  return request({
    url: `/work/order/getBeforeUpdateWorkInfo/${orderId}`,
    method: 'get',
  })
}

/**
 * 保存修改的工单信息
 * @param {OrderAPIRequest.OrderUpdateInfoForm} data - 更新后的工单信息
 * @param {String} workId - 工单id
 */
export function saveWorkInfo(
  data: OrderAPIRequest.OrderUpdateInfoForm,
  workId: string,
): AxiosPromise<APIResponse> {
  return request({
    url: `/work/order/saveWorkInfo/${workId}`,
    method: 'post',
    data,
  })
}

/**
 * 根据工单ID获取待审核的工单费用
 * @param id - 工单id
 */
export const getWaitCheckFeeByOrderId = (
  id: string,
): AxiosPromise<OrderAPIResponse.OrderFeeListByOrderId> => {
  return request({
    url: `/work/fee/getWaitCheckOrderFee/${id}`,
    method: 'get',
  })
}

/**
 * 工单费用批量审核
 * @param data - 请求参数
 */
export const orderFeeMultipleCheck = (
  data: OrderAPIRequest.FeeMultipleCheck,
): AxiosPromise<APIResponse> => {
  return request({
    url: '/work/fee/checkOrderFeeBatch',
    method: 'post',
    data,
  })
}

/**
 * 获取工单来源列表
 * @param {Object} data - 请求参数
 * @param {Number} data.limit - 每页显示数量
 * @param {Number} data.name - 搜索关键词
 * @param {String} data.page - 当面页码
 */
export const getSourceList = (data: {
  limit: number
  page: number
  name: string
}): AxiosPromise<OrderAPIResponse.SourceList> => {
  return request({
    url: '/work/source/getList',
    method: 'post',
    data,
  })
}

/**
 * 批量禁用或启用来源状态
 * @param ids - 工单来源ID集合
 * @param status - 来源状态：false-启用，true-禁用
 */
export const updateSourceStateByIds = (
  ids: Array<string>,
  status: boolean,
): AxiosPromise<APIResponse> => {
  return request({
    url: '/work/source/disable',
    method: 'post',
    data: ids,
    params: {
      status,
    },
  })
}

/**
 * 批量删除工单来源
 * @param ids - 工单来源ID集合
 */
export const delSourceByIds = (ids: Array<string>): AxiosPromise<APIResponse> => {
  return request({
    url: '/work/source/delete',
    method: 'delete',
    data: ids,
  })
}

/**
 * 修改默认来源
 * @param id - 工单来源ID
 */
export const updateDefaultSourceById = (id: string): AxiosPromise<APIResponse> => {
  return request({
    url: `/work/source/updateDefaultSource/${id}`,
    method: 'post',
  })
}

/**
 * 新增或修改工单来源
 * @param {OrderAPIRequest.SourceForm} data - 请求参数
 * @param {Boolean} data.defaultSelect - 是否默认选中：false-否，true-是
 * @param {String} data.id - 来源ID，新增不必传，修改必传
 * @param {String} data.name - 来源名称
 * @param {Boolean} data.status - 来源状态：false-启用，true-禁用
 */
export const sourceSave = (data: OrderAPIRequest.SourceForm): AxiosPromise<APIResponse> => {
  return request({
    url: '/work/source/save',
    method: 'post',
    data,
  })
}

/**
 * 获取来源详情
 * @param id - 来源id
 */
export const getSourceDetail = (id: string): AxiosPromise<OrderAPIResponse.SourceDetail> => {
  return request({
    url: `/work/source/getDetail/${id}`,
    method: 'get',
  })
}

/**
 * 获取来源下拉选项
 * @param {Object} data - 请求参数
 * @param {String} data.name - 搜索关键词
 * @param {Number} data.limit - 每页显示数量
 * @param {Number} data.page - 当面页码
 */
export const getSourceOptionList = (data: {
  limit: number
  page: number
  name: string
}): AxiosPromise<OrderAPIResponse.SourceOptionList> => {
  return request({
    url: '/order/select/getWorkSource',
    method: 'post',
    data,
  })
}

/**
 * 获开票信息
 * @param orderId - 工单id
 */
export const getBillInfo = (orderId: string): AxiosPromise<OrderAPIResponse.BillInfo> => {
  return request({
    url: `/work/order/getWorkTicketDetail/${orderId}`,
    method: 'get',
  })
}

/**
 * 新增或修改工单质保类别
 * @param data - 请求参数
 */
export const warrantyCategoryFormSave = (
  data: OrderAPIRequest.WarrantyCategoryForm,
): AxiosPromise<APIResponse> => {
  return request({
    url: '/work/warranty/category/save',
    method: 'post',
    data,
  })
}

/**
 * 工单质保类别列表
 * @param data - 请求参数
 * @param data.name - 搜索关键词
 * @param data.limit - 每页显示数量
 * @param data.page - 当面页码
 */
export const getWarranryCategoryList = (data: {
  limit: number
  page: number
  name: string
}): AxiosPromise<OrderAPIResponse.WarrantyCategoryList> => {
  return request({
    url: '/work/warranty/category/getList',
    method: 'post',
    data,
  })
}

/**
 * 根据工单质保类别id获取详情
 * @param {String} id - 质保类别id
 */
export const getOrderWarrantyCategoryDetail = (
  id: string,
): AxiosPromise<OrderAPIResponse.WarrantyCategoryDetail> => {
  return request({
    url: `/work/warranty/category/getDetail/${id}`,
    method: 'get',
  })
}

/**
 * 根据质保类别id删除质保类别
 * @param idList - 质保类别id集合
 */
export const delWarrantyCategoryByIds = (idList: Array<string>): AxiosPromise<APIResponse> => {
  return request({
    url: '/work/warranty/category/delete',
    method: 'delete',
    data: idList,
  })
}

/**
 * 根据工单质保类别ID批量启用或禁用工单
 * @param idList 质保类别ID集合
 * @param status 质保类别状态：false-启用，true-禁用
 */
export const updateWarrantyCategoryStatusByIds = (
  idList: Array<string>,
  status: boolean,
): AxiosPromise<APIResponse> => {
  return request({
    url: '/work/warranty/category/disable',
    method: 'post',
    data: idList,
    params: {
      status,
    },
  })
}

/**
 * 获取工单质保类别
 * @param data - 请求参数
 * @param data.name - 搜索关键词
 * @param data.page - 当面页码
 * @param data.limit - 每页显示数量
 */
export const getOrderWarrantyCategoryOptionList = (data: {
  name: string
  limit: number
  page: number
}): AxiosPromise<OrderAPIResponse.WarrantyCategoryOptionsList> => {
  return request({
    url: '/order/select/getWorkWarrantyCategory',
    method: 'post',
    data,
  })
}

/**
 * 获取打印信息
 * @param orderId - 工单id
 */
export const getOrderFeeDetailPrint = (
  orderId: string,
): AxiosPromise<OrderAPIResponse.DetailPrint> => {
  return request({
    url: `/work/order/getOrderFeeDetailPrint/${orderId}`,
    method: 'get',
  })
}

/**
 * 审核报价
 * @param {object} param - 请求参数
 * @param {boolean} param.checkResult - 审核结果：false-驳回，true-通过
 * @param {string} param.remark - 备注
 * @param {string} orderId - 工单id
 */
export const checkQuote = (
  param: { checkResult: boolean; remark: string },
  orderId: string,
): AxiosPromise<APIResponse> => {
  return request({
    url: `/work/spare/fee/checkSpareWithFee/${orderId}`,
    method: 'post',
    data: param,
  })
}

/**
 * 审核序列号
 * @param {object} data - 请求参数
 * @param {boolean} data.checkResult - 审核结果
 * @param {string} data.remark - 备注
 * @param {string} orderId - 工单id
 */
export const checkCode = (
  data: { checkResult: boolean; remark: string },
  orderId: string,
): AxiosPromise<APIResponse> => {
  return request({
    url: `/work/order/checkBindingSnCode/${orderId}`,
    method: 'post',
    data,
  })
}

/**
 * 更改物料的序列号
 * @param {Array<{ snCode: string; workSapreMaterialId: string; workSpareId: string }>} data - 请求参数
 * @param {string} orderId - 工单id
 */
export const updateMaterialSnCode = (
  data: Array<{ snCode: string; workSapreMaterialId: string; workSpareId: string }>,
  orderId: string,
): AxiosPromise<APIResponse> => {
  return request({
    url: `/work/spare/fee/updateWorkSpareMaterial/${orderId}`,
    method: 'post',
    data,
  })
}

/**
 * 申请结单（部门角色使用）
 * @param {OrderAPIRequest.EndOrderApply} data - 请求参数
 * @param {string} orderId - 工单id
 */
export const engineerEndOrderApply = (
  data: OrderAPIRequest.EndOrderApply,
  orderId: string,
): AxiosPromise<APIResponse> => {
  return request({
    url: `/work/order/networkApplyFinishOrder/${orderId}`,
    method: 'post',
    data,
  })
}

/**
 * 确定领料（部门角色使用）
 * @param {string} orderId - 工单id
 */
export const engineerTakeMaterialConfirm = (orderId: string): AxiosPromise<APIResponse> => {
  return request({
    url: `/work/order/saveToPickingOrderMaterial/${orderId}`,
    method: 'post',
  })
}

/**
 * 根据工单id审核已申请开票的工单
 * @param {Array<string>} ids - 工单id集合
 */
export const checkApplyedTicketOrderByOrderIds = (
  ids: Array<string>,
): AxiosPromise<APIResponse> => {
  return request({
    url: '/work/module/invoice',
    method: 'post',
    data: ids,
  })
}

/**
 * 获取所有工单的费用合计
 */
export const getOrderListTotalFee = (
  data: OrderAPIRequest.GetList,
): AxiosPromise<
  NewAPIResponse<{ serviceFee: number; orderTotalFee: number; materialFee: number }>
> => {
  return request({
    url: '/work/order/getOrderFee',
    method: 'post',
    data,
  })
}

/**
 * 获取工单详情的默认质保类别
 * @param {object} data - 请求参数
 * @param {string} data.id - 序列号id
 * @param {number} data.limit - 每页显示数量
 * @param {string} data.name - 搜索关键字
 * @param {number} data.page - 当前页码
 */
export const getOrderDetailWarrantyTypeDefault = (data: {
  id: string
  limit: number
  name: string
  page: number
}): AxiosPromise<NewAPIResponse<OrderDetailDefaultWarrantyType>> => {
  return request({
    url: '/order/select/getWorkWarrantyCategoryV2',
    method: 'post',
    data,
  })
}

/**
 * 获取结单列表
 * @param {string} id - 工单id
 */
export const getEndOrderFieldList = (
  id: string,
): AxiosPromise<OrderAPIResponse.OrderEndFieldList> => {
  return request({
    url: `/work/finalizing/bill/${id}`,
    method: 'get',
  })
}

/**
 * 保存、修改、审核结单信息
 * @param {string} id - 工单id
 * @param {0|1|2|3} type - 保存类型，0->申请 1->审核通过 2->审核驳回 3->保存并审核
 * @param {string} moduleId - 工单模块id
 * @param {Array<OrderEndFieldListItem['objects']>} data - 请求参数
 */
export const saveEndOrderResult = (
  id: string,
  type: 0 | 1 | 2 | 3,
  moduleId: string,
  data: OrderEndFieldListItem['objects'],
): AxiosPromise<APIResponse> => {
  return request({
    url: `/work/finalizing/bill/${moduleId}/${id}/${type}`,
    method: 'post',
    data,
  })
}

/**
 * 在结单界面获取工单模板：根据工单类型和工单ID获取工单模板
 * @param {string} moduleId - 工单模块id
 * @param {string} orderId - 工单id
 */
export const getTemplateByModuleAndOrderId = (
  moduleId: string,
  orderId: string,
): AxiosPromise<NewAPIResponse<OrderTemplateForUpdateEndTemplate>> => {
  return request({
    url: `/work/finalizing/bill/${moduleId}/${orderId}`,
    method: 'get',
  })
}

/**
 * 获取工单结单状态
 * @param {string} id - 工单id
 */
export const getOrderEndStatus = (id: string): AxiosPromise<NewAPIResponse<boolean>> => {
  return request({
    url: `/work/finalizing/bill/status/${id}`,
    method: 'get',
  })
}

/**
 * 分派/改派工程师
 * @param {OrderDetailAPiRequest.AssignEngineerForm} data - 请求参数
 * @param {string} data.networkEmpId - 服务商人员id
 * @param {string} data.networkId - 服务商id
 * @param {string} data.remark - 派单备注
 * @param {Array<string>} data.workIds - 工单id集合
 */
export const assignEngineer = (
  data: OrderDetailAPiRequest.AssignEngineerForm,
): AxiosPromise<APIResponse> => {
  return request({
    url: '/work/order/orderDispatchEngineer',
    method: 'put',
    data,
  })
}

/**
 * 部门确认接单
 * @param {object} data - 请求参数
 * @param {string} data.id - 工单id
 * @param {string} data.networkId - 部门id
 */
export const networkConfirmAcceptOrderApi = (data: {
  id: string
  networkId: string
}): AxiosPromise<APIResponse> => {
  return request({
    url: '/work/order/orderNetwork/confirm',
    method: 'post',
    data,
  })
}

/**
 * 工程师确认接单
 * @param {object} data - 请求参数
 * @param {string} data.id - 工单id
 * @param {string} data.networkId - 部门id
 * @param {string} data.engineerId - 工程师id
 */
export const engineerConfirmAcceptOrderApi = (data: {
  id: string
  networkId: string
  engineerId: string
}): AxiosPromise<APIResponse> => {
  return request({
    url: '/work/order/orderNetwork/engineer/confirm',
    method: 'post',
    data,
  })
}

/**
 * 获取客户替换列表
 * @param keyword - 查询关键字
 */
export const getReplaceCustomerList = (
  keyword: string,
): AxiosPromise<APIResponse<Array<OrderDetailAPIResponse.ReplaceCustomer>>> => {
  return request({
    url: '/user/contact/selectName',
    method: 'post',
    data: {
      name: keyword,
    },
  })
}

/**
 * 获取编辑之前的工单费用信息（获取全部的）
 * @param {string} orderId - 工单id
 */
export const getOrderAllFeeInfoBeforeEdit = (
  orderId: string,
): AxiosPromise<APIResponseRows<Array<OrderFeeAPIResponse.FeeInfoBeforeEdit>>> => {
  return request({
    url: `/work/fee/getOrderFeeDetailV2/${orderId}`,
    method: 'get',
  })
}

/**
 * 更新工单详情的费用信息
 * @param {OrderFeeAPIRequest.UpdateOrderDetailFee} data - 更新表单
 */
export const updateOrderDetailFee = (
  data: OrderFeeAPIRequest.UpdateOrderDetailFee,
): AxiosPromise<APIResponse> => {
  return request({
    url: '/work/fee/update',
    method: 'post',
    data,
  })
}

/**
 * 根据工单ID获取催退留言
 * @param {Object} data - 请求参数
 * @param {number} data.limit - 一页显示多少条
 * @param {number} data.page - 当前页码
 * @param {string} orderId - 工单id
 */
export function getOrderUrgeReturnMessageList(
  data: { limit: number; page: number },
  orderId: string,
) {
  return request({
    url: `/work/order/getWorkPushbackMessageList/${orderId}`,
    method: 'post',
    data,
  })
}

/**
 * 保存工单催退留言
 * @param data - 请求参数
 * @param data.annex - 附件
 * @param data.content - 留言内容
 * @param data.visitTypes - 请求参数
 * @param data.workId - 工单ID
 */
export function saveOrderUrgeReturnMessage(data: {
  annex: Array<UploadRawFile>
  content: string
  visitTypes: Array<0 | 1 | 2 | 3 | 4>
  workId: string | unknown[]
}) {
  return request({
    url: '/work/order/saveWorPushbackMessage',
    method: 'post',
    data,
  })
}

/** 保存工单备注
 * @param {object} data - 请求参数
 * @param {Array<any>} data.annexs - 工单备注附件
 * @param {string} data.remark - 工单备注
 * @param {0 | 1 | 2 | 3 | 4} data.visitTypes - 可见类型：0-公开，1-部门，2-工程师，3-工单公司，4-工单客服
 * @param {Array<string>} data.workIds - 工单ID参数
 */
export function multipleSaveOrderUrgeReturn(
  data: OrderAPIRequestParam.MultipleSaveOrderUrgeReturn,
) {
  return request({
    url: '/ork/remark/pushback/save',
    method: 'post',
    data,
  })
}

/**
 * 根据编码查询，用于工单详情更换序列号
 * @param {string} snCode - 序列号
 */
export const queryCodeForUpdateCode = (
  snCode: string,
): AxiosPromise<NewAPIResponse<OrderDetailAPIResponse.SnCodeInfoForOrderDetailUpdateSnCode>> => {
  return request({
    url: `/code/${snCode}`,
    method: 'get',
  })
}
